//: Playground - noun: a place where people can play

import UIKit

// 函数的声明与调用
func sayHello(personName:String) ->String{
    
    let greeting = "Hello, " + personName + "!"
    return greeting
}

print(sayHello("Anna"))

//外部参数名

func someFunction(externalParameterName localParameterName: Int) {
  
    
}

someFunction(externalParameterName: 0)

func containsCharacter(var stingTo: String, characterToFind: Character) -> Bool {
    stingTo = "10"
    for character in stingTo.characters {
        if character == characterToFind {
            return true
        }
    }
    return false
}

//函数类型

func addTwoInts(a:Int,b:Int)->Int{

    return a+b
}

func addTwoInts(a:Int) ->Int{
    
    return a
}

func multipkyTwoInts(a:Int,b:Int)->Int{
    
    return a*b
}
//这两个函数的类型是(Int, Int)->Int可以解释为
var mathFunction:(Int,Int)->Int = addTwoInts

print(("Result:\(mathFunction(2,3))"))

mathFunction = multipkyTwoInts

print(("Result:\(mathFunction(2,3))"))

//函数类型的参数
typealias FunctionType = (Int,Int)->Int

func printaMathResult(mathFunction:FunctionType,a:Int,b:Int){
    
    print("Result:\(mathFunction(a,b))")
    
}

printaMathResult(addTwoInts, a: 1, b: 3)

//函数类型的返回值

func choosestepFunction(backwards:Bool)->FunctionType{
    
    return backwards ? addTwoInts : multipkyTwoInts
}

//嵌套函数
func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
    func stepForward(input: Int) -> Int { return input + 1 }
    func stepBackward(input: Int) -> Int { return input - 1 }
    return backwards ? stepBackward : stepForward
}
var currentValue = -4
let moveNearerToZero = chooseStepFunction(currentValue > 0)
// moveNearerToZero now refers to the nested stepForward() function
while currentValue != 0 {
    print("\(currentValue)... ")
    currentValue = moveNearerToZero(currentValue)
}
print("zero!")




